﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Domain.Models;
using One.Web;
using System.Web.Security;
using Utilities;
using Domain.DAL;

namespace WebApp
{
    public partial class SignIn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                usernameTextBox.Focus();
            }
        }

        protected void loginButton_Click(object sender, EventArgs e)
        {
            try
            {
                string username = usernameTextBox.Text;
                string password = Utility.HashMD5(passwordTextBox.Text);

                UserDAO uDAO = UserDAO.GetInstance();
                if (uDAO != null)
                {
                    Domain.Models.User u = new Domain.Models.User();
                    u = uDAO.ValidateUser(username, password);
                    if (u != null && u.ID != MyConfig.NullNumber)
                    {
                        messageLabel.Visible = false;

                        string rolesName = uDAO.GetRoles(u.ID);
                        string separateString = GetParameterValue(MyConfig.SeparateStringCode);

                        if (!string.IsNullOrEmpty(rolesName) && !string.IsNullOrEmpty(separateString))
                        {
                            string[] roles = rolesName.Split(new string[] { separateString }, StringSplitOptions.RemoveEmptyEntries);// lay tat ca role cua employee;
                            FormsAuthenticationHelper.RedirectFromLoginPage(username,
                                                                                roles,
                                                                                FormsAuthentication.DefaultUrl,
                                                                                false);
                        }
                    }
                    else
                    {
                        messageLabel.Visible = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Utility.ShowMessageBox(this.Page, ex.ToString());
            }
        }

        private string GetParameterValue(string code)
        {
            string value = string.Empty;
            try
            {
                Domain.DAL.ParameterDAO pDAO = Domain.DAL.ParameterDAO.GetInstance();
                if (pDAO != null)
                {
                    Domain.Models.Parameter p = new Domain.Models.Parameter();

                    p = pDAO.GetByCode(code);
                    if (p != null && !string.IsNullOrEmpty(p.Value))
                    {
                        value = p.Value;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return value;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(this.ResolveUrl("~/Default.aspx"));
        }
    }
}